//
// Created by 张玭 on 24-6-13.
//
# include "iostream"

# define MAX 1000
using namespace std;

struct Person {
    /*联系人结构体*/
    std::string m_Name;
    bool m_Sex = false;
    int m_Age = 0;
    std::string m_Phone;
    std::string m_Address;
};

struct Addressbooks {
    /*通讯录结构体*/
    struct Person personArray[MAX];
    int m_Size;
};

void showMenu() {
    /*显示主菜单*/
    cout << "**********************" << endl;
    cout << "***** 1.添加联系人 *****" << endl;
    cout << "***** 2.显示联系人 *****" << endl;
    cout << "***** 3.删除联系人 *****" << endl;
    cout << "***** 4.查找联系人 *****" << endl;
    cout << "***** 5.修改联系人 *****" << endl;
    cout << "***** 6.清空联系人 *****" << endl;
    cout << "***** 7.退出通讯录 *****" << endl;
    cout << "**********************" << endl;
}

void clearScreen() {
    cout << "按任意键继续";
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.get();
    system("clear"); // 清屏
}


void printPerson(const Person &person) {
    cout << "**********************" << endl;
    cout << "姓名：" << person.m_Name << endl;
    cout << "性别：" << (person.m_Sex ? "男" : "女") << endl;
    cout << "年龄：" << person.m_Age << endl;
    cout << "电话：" << person.m_Phone << endl;
    cout << "地址：" << person.m_Address << endl;
}

void addPerson(Addressbooks *abs) {
    /*添加联系人*/
    if (abs->m_Size == MAX) {
        cout << "通讯录已满，无法添加" << endl;
        return;
    }
    Person tmp;
    //添加联系人
    //1.姓名
    cout << "请输入姓名：" << endl;
    cin >> tmp.m_Name;
    //2.性别
    int sex;
    bool flag = true;
    while (flag) {
        cout << "请输入性别：0(男性) 1(女性)" << endl;
        cin >> sex;
        switch (sex) {
            case 0:
                tmp.m_Sex = true;
                flag = false;
                break;
            case 1:
                tmp.m_Sex = false;
                flag = false;
                break;
            default:
                cout << "输入有误" << endl;

        }
    }
    //3.年龄
    int age;
    flag = true;
    while (flag) {
        cout << "请输入年龄：(0-150)" << endl;
        cin >> age;
        if (age < 0 || age > 150) {
            cout << "输入有误" << endl;
        } else {
            tmp.m_Age = age;
            flag = false;
        }
    }
    //4.电话
    cout << "请输入电话：" << endl;
    cin >> tmp.m_Phone;
    //5.地址
    cout << "请输入地址：" << endl;
    cin >> tmp.m_Address;


    //打印联系人
    printPerson(tmp);

    char order;
    cout << "是否确认添加联系人？(Y/N)" << endl;
    cin >> order;
    if (order != 'Y' and order != 'y') {
        cout << "取消添加联系人" << endl;
        return;
    }
    //将联系人信息保存到通讯录中
    cout << "添加联系人成功" << endl;
    abs->personArray[abs->m_Size] = tmp;
    abs->m_Size++;
    clearScreen();
}

void showPerson(Addressbooks *abs) {
    /*显示联系人*/
    if (abs->m_Size == 0) {
        cout << "通讯录为空" << endl;
    } else {
        cout << "通讯录中共有" << abs->m_Size << "个联系人:" << endl;
        for (int i = 0; i < abs->m_Size; i++) {
            printPerson(abs->personArray[i]);
        }
    }
}

int isExist(Addressbooks *abs, const string &name) {
    /*判断联系人是否存在*/
    for (int i = 0; i < abs->m_Size; i++) {
        if (abs->personArray[i].m_Name == name) {
            return i;
        }
    }
    return -1;
}

void deletePerson(Addressbooks *abs) {
    /*删除联系人*/
    if (abs->m_Size == 0) {
        cout << "通讯录为空" << endl;
    } else {
        cout << "请输入要删除的联系人姓名：" << endl;
        string name;
        cin >> name;
        int index = isExist(abs, name);
        if (index == -1) {
            cout << "联系人不存在" << endl;
            clearScreen();
            return;
        } else {
            cout << "是否确认删除联系人？(Y/N)" << endl;
            printPerson(abs->personArray[index]);
            char order;
            cin >> order;
            if (order == 'Y' or order == 'y') {
                cout << "确认删除联系人" << endl;
                //将后面的向前移动
                for (int j = index; j < abs->m_Size; j++) {
                    abs->personArray[j] = abs->personArray[j + 1];
                }
                abs->m_Size--;
                cout << "删除联系人成功" << endl;
                return;
            } else {
                cout << "取消删除联系人" << endl;
                return;
            }

        }
    }
    clearScreen();

}

void findPerson(Addressbooks *abs) {
    /*查找联系人*/
    cout << "请输入要查找的联系人姓名：" << endl;
    string name;
    cin >> name;
    int index = isExist(abs, name);
    if (index == -1) {
        cout << "联系人不存在" << endl;
        clearScreen();
        return;
    } else {
        cout << "查找到联系人" << endl;
        printPerson(abs->personArray[index]);
    }
}

void modifyPerson(Addressbooks *abs) {
    /*修改联系人*/
    cout << "请输入要修改的联系人姓名：" << endl;
    string name;
    cin >> name;
    int index = isExist(abs, name);
    if (index == -1) {
        cout << "联系人不存在" << endl;
        clearScreen();
        return;
    } else {
        cout << "查找到联系人" << endl;
        printPerson(abs->personArray[index]);
        cout << "请输入要修改的信息" << endl;
        cout << "***** 1.修改姓名 *****" << endl;
        cout << "***** 2.修改年龄 *****" << endl;
        cout << "***** 3.修改性别 *****" << endl;
        cout << "***** 4.修改电话 *****" << endl;
        cout << "***** 5.修改地址 *****" << endl;
        cout << "***** 6.修改完成 *****" << endl;
        int select = 0;
        bool flag = true;
        while (flag) {
            cout << "请输入您的选择：" << endl;
            cin >> select;
            switch (select) {
                case 1:
                    cout << "修改姓名" << endl;
                    cout << "请输入新的姓名：" << endl;
                    cin >> abs->personArray[index].m_Name;
                    cout << "修改成功" << endl;
                    break;
                case 2:
                    cout << "修改年龄(0-150)" << endl;
                    cout << "请输入新的年龄：" << endl;
                    int age;
                    cin >> age;
                    if (age > 150 or age < 0) {
                        cout << "错误的年龄范围" << endl;
                        break;
                    } else {
                        abs->personArray[index].m_Age = age;
                        cout << "修改成功" << endl;
                        break;
                    }
                case 3:
                    cout << "修改性别" << endl;
                    cout << "请输入新的性别：0(男性) 1(女性)" << endl;
                    int sex;
                    cin >> sex;
                    if (sex == 0 or sex == 1) {
                        abs->personArray[index].m_Sex = sex == 0;
                        cout << "修改成功" << endl;

                    } else {
                        cout << "错误的输入" << endl;
                    }
                    break;
                case 4:
                    cout << "修改电话" << endl;
                    cout << "请输入新的电话" << endl;
                    cin >> abs->personArray[index].m_Phone;
                    cout << "修改成功" << endl;
                    break;
                case 5:
                    cout << "修改地址" << endl;
                    cout << "请输入新的地址" << endl;
                    cin >> abs->personArray[index].m_Address;
                    cout << "修改成功" << endl;
                    break;
                default:
                    cout << "修改完成" << endl;
                    flag = false;
                    printPerson(abs->personArray[index]);
            }
        }
    }
    clearScreen();
}

int main() {
    //初始创建通讯录
    Addressbooks abs;
    abs.m_Size = 0;

    //show main menu
    int select = 0;//创建用户选择输入的变量
    while (true) {
        showMenu();
        cout << "请输入您的选择：" << endl;
        cin >> select;
        switch (select) {
            case 1:
                cout << "添加联系人" << endl;
                /*利用地址传递可以修饰实参*/
                addPerson(&abs);
                break;
            case 2:
                cout << "显示联系人" << endl;
                showPerson(&abs);
                break;
            case 3:
                cout << "删除联系人" << endl;
                deletePerson(&abs);
                break;
            case 4:
                cout << "查找联系人" << endl;
                findPerson(&abs);
                break;
            case 5:
                cout << "修改联系人" << endl;
                modifyPerson(&abs);
                break;
            case 6:
                cout << "清空联系人" << endl;
                abs.m_Size = 0;
                cout << "完成清空" << endl;
                clearScreen();
                break;
            case 7:
                cout << "退出通讯录" << endl;
                return 0;
            default:
                cout << "输入有误" << endl;
                break;
        }
    }


}